﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LoanPrequalificationSystem.Model.LoanApplications;

namespace LoanPrequalificationSystem.Model.Products
{
    
    public class Product
    {
        private ProductIsAvailableSpecification _productIsAvailable;

        public Product()
        {
            _productIsAvailable = new ProductIsAvailableSpecification(this,new LoanMonthlyPaymentCalculator());
        }
        public bool  IsAvailableFor(LoanApplication loanApplication)
        {
            return _productIsAvailable.IsSatisfiedBy(loanApplication);
        }

        private Guid _id;
        private string _name;
        private float _interestRate;
        private float _maximumLTV;
        private float _maximumLoan;
        private int _maximumLoanTerm;

        public Guid Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        public float InterestRate
        {
            get
            {
                return _interestRate;
            }
            set
            {
                _interestRate = value;

            }
        }
        public float MaximumLTV {
            get {
                return _maximumLTV;
            }
            set {
                _maximumLTV = value;
            }
        }
        public float MaximumLoan
        {
            get
            {
                return _maximumLoan;
            }
            set {
                _maximumLoan = value;
            }
        }
        public int MaximumLoanTerm {
            get { return _maximumLoanTerm;
            }
            set { _maximumLoanTerm = value;
            }
        }

        public List<BrokenBusinessRule> GetBrokenRules() {
            List<BrokenBusinessRule> brokenRules = new List<BrokenBusinessRule>();
            if (string.IsNullOrEmpty(Name)) {
                brokenRules.Add(new BrokenBusinessRule("Name", "A Product Must have a name."));
            }
            if (!IsValidInterestRate())
            {
                brokenRules.Add(new BrokenBusinessRule("InterestRate", "product's interest rate must be of 0.25."));
            }
            return brokenRules;
        
        }

        private bool IsValidInterestRate()
        {
            bool valid = true;
            if (InterestRate > 0) {
                if (InterestRate % 0.125 != 0) 
                {
                    valid = false;
                }
            }
            else
            {
                valid = false;
            }
            return valid;
        }
    }
}
